package com.itheima.system.controller;

import com.itheima.common.controller.BaseController;
import com.itheima.common.entity.Result;
import com.itheima.common.entity.ResultCode;
import com.itheima.system.pojo.Role;
import com.itheima.system.pojo.vo.AssignPremVo;
import com.itheima.system.service.PermissionService;
import com.itheima.system.service.RoleService;
import com.itheima.common.utils.BeanMapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;
import java.util.Set;

/**
 * @author IntelliJ IDEA
 * @date 2020/10/26  9:21
 *
 * 角色增删改查
 * @CrossOrigin //解决ajax跨域
 */

@RestController
@RequestMapping("/sys")
@CrossOrigin
public class RoleController extends BaseController {

    @Autowired
    private RoleService roleService;

    @Autowired
    private PermissionService permissionService;

    /**
     * 分配权限
     */
    @PutMapping("/role/assignPrem")
    public Result assignPrem(@RequestBody AssignPremVo assignPremVo){
        try{
            roleService.assignPrem(assignPremVo);
            return new Result(ResultCode.SUCCESS);
        }catch (Exception e){
            return new Result(ResultCode.FAIL);
        }
    }

    /**
     * 添加角色
     */
    @PostMapping("/role")
    public Result addRole(@RequestBody Role role){
        role.setCompanyId(this.companyId);
        Integer rows=roleService.addRole(role);
        if(rows<1){
            return new Result(ResultCode.FAIL);
        }
        return new Result(ResultCode.SUCCESS);
    }

    /**
     * 更新角色
     */
    @PutMapping("/role/{id}")
    public Result updateRoleByRoleId(@PathVariable("id") String id, @RequestBody Role role){
        role.setId(id);
        Integer rows=roleService.updateRoleByRoleId(role);
        if(rows<1){
            return new Result(ResultCode.FAIL);
        }
        return new Result(ResultCode.SUCCESS);
    }

    /**
     * 删除角色
     */
    @DeleteMapping("/role/{id}")
    public Result deleteRoleById(@PathVariable("id") String id){
        Integer rows=roleService.deleteRoleById(id);
        if(rows<1){
            return new Result(ResultCode.FAIL);
        }
        return new Result(ResultCode.SUCCESS);
    }

    /**
     * 根据id查询角色信息
     */
    @GetMapping("/role/{id}")
    public Result findRoleById(@PathVariable("id") String id){
        //查询角色
        Role role = roleService.findRoleById(id);
        //Bean  ---> map
        Map<String, Object> map = BeanMapUtils.beanToMap(role);
        //根据roleId查询角色下所有的权限
        Set<String> permIds=permissionService.findPermIdsByRoleId(role.getId());
        map.put("permIds",permIds);
        return new Result(ResultCode.SUCCESS,map);
    }

    /**
     * 分页查询角色
     */
    @GetMapping("/role")
    public Result findAllRoleByPage(@RequestParam("page") Integer page,@RequestParam("pagesize") Integer pagesize){
        return new Result(ResultCode.SUCCESS,roleService.findAllRoleByPage(this.companyId,page,pagesize));
    }

    /**
     * 根据企业id，查询角色列表
     */
    @GetMapping("/role/list")
    public Result findAllRole(){
        return new Result(ResultCode.SUCCESS,roleService.findAllRole(this.companyId));
    }

}
